import request from "@/utils/request";
import { IObject } from "../common/common";

const BASE_URL = "/business/purchase";

const PurchaseAPI = {
  /** 获取分页数据 */
  getPage(params?: PurchasePageQuery) {
    return request<any, PageResult<PurchasePageVO[]>>({
      url: `${BASE_URL}/loadPage`,
      method: "get",
      params,
    });
  },
  /** 加载自己的 */
  loadOwn(params?: PurchasePageQuery) {
    return request<any, PageResult<PurchasePageVO[]>>({
      url: `${BASE_URL}/loadOwn`,
      method: "get",
      params,
    });
  },

  /** 加载选择项 */
  loadChoose(params?: PurchasePageQuery) {
    return request<any, PageResult<PurchasePageVO[]>>({
      url: `${BASE_URL}/loadChoose`,
      method: "get",
      params,
    });
  },
  /**
   * 根据id查询
   */
  load(id: string) {
    return request<any, PurchaseDetail>({
      url: `${BASE_URL}/load/${id}`,
      method: "get",
    });
  },
  /**
   * 创建采购申请
   * @param data 表单数据
   */
  create(data: PurchaseForm) {
    return request<any, PurchasePageVO>({
      url: `${BASE_URL}/add`,
      method: "post",
      data,
    });
  },
  /**
   * 修改采购申请
   * @param data 表单数据
   */
  update(data: PurchaseForm) {
    return request<any, PurchasePageVO>({
      url: `${BASE_URL}/update`,
      method: "post",
      data,
    });
  },
  /**
   * 删除采购申请
   */
  deleteById(id: string) {
    return request({
      url: `${BASE_URL}/delete/${id}`,
      method: "get",
    });
  },
  /**
   * 启动采购流程
   */
  start(params: { id: string; flowId: string }) {
    return request({
      url: `${BASE_URL}/startActivity`,
      method: "get",
      params,
    });
  },

  /**
   * 采购申请单审核
   */
  review(data: PurchaseReviewVO) {
    return request<PurchasePageVO, any>({
      url: `${BASE_URL}/review`,
      method: "post",
      data,
    });
  },

  /**
   * 采购申请单配送
   */
  apply(data: PurchaseApplyVO) {
    return request<PurchaseApplyVO, any>({
      url: `${BASE_URL}/apply`,
      method: "post",
      data,
    });
  },
  /**
   * 采购
   */
  purchase(data: PurchaseVO) {
    return request<PurchaseApplyVO, any>({
      url: `${BASE_URL}/purchase`,
      method: "post",
      data,
    });
  },
};

export default PurchaseAPI;

/** 筛选条件 */
export interface PurchasePageQuery extends PageQuery {
  /** 状态 */
  statuses?: string;
  /** 类型 */
  type?: string;
}

/** 实例分页对象 */
export interface PurchasePageVO {
  /** ID */
  id: string;
  /** 流程模板ID */
  activityId: string;
  /** 合同ID */
  contractId: string;
  /** 名称 */
  title: string;
  /** 描述 */
  remark: string;
  /** 采购金额 */
  amount: number;
  /** 创建时间 */
  createTime: string;
  /** 申报人 */
  userId: string;
  username: string;
  /** 快递单号 */
  deliveryNo: string;
  /** 快递公司 */
  deliveryCompany: string;
  /** 状态 */
  status: string;
}

export interface PurchaseDetail extends PurchasePageVO {
  /** 采购申请单 */
  items: PurchaseItemVO[];
  /** 最新的申请记录 */
  lastRecord?: PurchaseRecord;
  itemTypeAmounts: ItemTypeAmount[];
}

export interface ItemTypeAmount {
  type: string;
  typeName: string;
  contractAmount: number;
  applyAmount: number;
  compare: number;
}

export interface PurchaseForm {
  /** ID */
  id?: string;
  /** 合同ID */
  contractId: string;
  /** 名称 */
  title: string;
  /** 描述 */
  remark?: string;
  /** 采购金额 */
  amount: string;
  /** 采购申请单 */
  items: PurchaseItemVO[];
  type: string;
  /** 申请说明 */
  applyMark: string;
}

export interface PurchaseItemVO {
  /** 节点ID */
  id?: string;
  /** 关联申购id */
  purchaseId?: string;
  /** 名称 */
  name: string;
  /** 描述 */
  remark: string;
  /** 采购金额 */
  amount: string | number;
  /** 数量 */
  quantity: number;
  spec: string;
  unit: string;
  code?: string;
  materialId: string;
  materialType: string;
  /* 已申报 */
  totalAmount?: string;
  totalQuantity?: number;
  /* 合同数量金额 */
  contractPrice?: string;
  contractQuantity?: number;
}

export enum PurchaseStatus {
  CREATE = "CREATE",
  APPLY_BACK = "APPLY_BACK",
  APPLY = "APPLY",
  PURCHASE = "PURCHASE",
  END = "END",
}

export const Purchase_Status_Label: IObject = {
  [PurchaseStatus.CREATE]: "创建中",
  [PurchaseStatus.APPLY_BACK]: "驳回",
  [PurchaseStatus.APPLY]: "申请中",
  [PurchaseStatus.PURCHASE]: "采购中",
  [PurchaseStatus.END]: "结束",
};

export const Purchase_Apply_Label: IObject = {
  ["0"]: "未审核",
  ["1"]: "通过",
  ["2"]: "驳回",
};

/** 申请表单 */
export interface PurchaseApplyVO {
  /** ID */
  id: string;
}

/** 审核表单 */
export interface PurchaseReviewVO {
  /** ID */
  recordId: string;
  /** 审核结果 */
  result: string;
  /** 审批说明 */
  reviewMark: string;
}

/** 采购申请单审核表单 */
export interface PurchaseVO {
  /** ID */
  id: string;
  /** 配送单号 */
  deliveryCompany: string;
  /** 配送单号 */
  deliveryNo: string;
}

/** 申购申请单 */
export interface PurchaseRecord {
  /** ID */
  id: string;
  /** 采购申请单 */
  purchaseId: string;
  /** 申请人id */
  applyUserId: string;
  /** 申请人 */
  applyUsername: string;
  /** 申请时间 */
  applyTime: string;
  /** 申请说明 */
  applyMark: string;
  /** 审核人id */
  reviewUserId: string;
  /** 审核人 */
  reviewUsername: string;
  /** 审核时间 */
  reviewTime: string;
  /** 审核说明 */
  reviewMark: string;
  /** 审核结果 0: 未审批 1: 通过 2: 未通过 */
  result: number;
}
